﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// AddEnToChEntry.xaml 的交互逻辑
    /// </summary>
    public partial class AddEnToChEntryWindow : MetroWindow
    {
        /// <summary>
        /// [构造方法]构造“添加词条”窗口。
        /// </summary>
        public AddEnToChEntryWindow()
        {
            InitializeComponent();
            this.Title = Globals.AppName + " - 添加词条";
            this.Loaded += AddEnToChEntryWindow_Loaded;
        }

        /// <summary>
        /// 载入窗口时自动定位光标。
        /// </summary>
        private void AddEnToChEntryWindow_Loaded(object sender, RoutedEventArgs e)
        {
            tbxCh.Focus();
        }

        /// <summary>
        /// 添加词条到对应文本文件中。
        /// </summary>
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (ckxAddToUserDict.IsChecked == false &&
                ckxAddToWorkspace.IsChecked == false)
            {
                LMessageBox.Show("至少选择【添加到用户词典】或【添加到工作区词典】之一。", Globals.AppName,
                     MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            //Format And Validate
            string chinese;
            string english;
            string alpha;
            string comment;
            string validateText = FormatAndValidate(out english, out chinese, out alpha, out comment);

            if (string.IsNullOrWhiteSpace(validateText) == false)
            {
                LMessageBox.Show("请按照要求填写相关内容：" + validateText, Globals.AppName, MessageBoxButton.OK,
                     MessageBoxImage.Warning);
                return;
            }

            if (ckxAddToUserDict.IsChecked == true)
            {
                System.IO.File.AppendAllLines(Globals.PathOfUserDictionary, new string[] { $"{english}\t{chinese}\t{alpha}\t{comment}" });
            }

            if (ckxAddToWorkspace.IsChecked == true)
            {
                System.IO.File.AppendAllLines(Globals.PathOfWorkspaceDictionary, new string[] { $"{english}\t{chinese}\t{alpha}\t{comment}" });
            }

            this.OnEntryAdded(this, new EntryAddedEventArgs()
            {
                Entry = new EnToChEntry()
                {
                    Chinese = tbxCh.Text,
                    English = tbxEn.Text,
                    Alpha = tbxAlpha.Text,
                    Comment = tbxComment.Text,
                }
            });
            this.Close();
        }

        /// <summary>
        /// 格式化并检验词条。
        /// </summary>
        /// <param name="english">英文。</param>
        /// <param name="chinese">中文。</param>
        /// <param name="alpha">英文字母缩写。</param>
        /// <param name="comment">注释文本。</param>
        private string FormatAndValidate(out string english, out string chinese, out string alpha, out string comment)
        {
            var vResult = new StringBuilder();

            var spaceChars = new char[] { ' ', '　', '\t' };
            english = tbxEn.Text.Trim(spaceChars);
            chinese = tbxCh.Text.Trim(spaceChars);
            alpha = tbxAlpha.Text.Trim(spaceChars);
            comment = tbxComment.Text.Trim(spaceChars);

            Regex regex = new Regex(@"[ 　\t]{2,}");

            english = regex.Replace(english, " ");
            if (string.IsNullOrWhiteSpace(english)) vResult.Append("英文原词不能为空白字符！\r\n");

            chinese = regex.Replace(chinese, " ");
            if (string.IsNullOrWhiteSpace(chinese)) vResult.Append("中文译词不能为空白字符！\r\n");

            alpha = regex.Replace(alpha, " ");//.ToUpper(); //如WiFi这样的缩写就不能全大写！
            if (string.IsNullOrWhiteSpace(alpha)) vResult.Append("中文译词不能为空白字符！\r\n");

            return vResult.ToString(); ;
        }

        /// <summary>
        /// 词条被添加事件。
        /// </summary>
        public event EventHandler<EntryAddedEventArgs> EntryAdded;

        /// <summary>
        /// 触发词条被添加事件。
        /// </summary>
        protected void OnEntryAdded(object sender, EntryAddedEventArgs e)
        {
            if (EntryAdded != null)
            {
                EntryAdded(sender, e);
            }
        }

        /// <summary>
        /// 放弃添加词条。
        /// </summary>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

    /// <summary>
    /// 词条被添加事件所使用的参数。
    /// </summary>
    public class EntryAddedEventArgs : EventArgs
    {
        public EnToChEntry Entry { get; set; }
    }
}
